package domain

import (
	"github.com/labstack/echo/v4"
)

type OrderResource struct {
	cache *CacheService
}

func NewOrder(cache *CacheService) *OrderResource {
	return &OrderResource{
		cache: cache,
	}
}

func (o *OrderResource) Neworder(c echo.Context) error {

	data := &Data{}
	err := c.Bind(data)
	if err != nil {
		return err
	}
	err = o.cache.Add(data)
	if err != nil {
		return c.String(500, err.Error())
	}
	return c.String(200, "add success")
}

func (o *OrderResource) Queryorders(c echo.Context) error {

	data, err := o.cache.Query()
	if err != nil {
		return c.String(500, err.Error())
	}
	return c.JSON(200, data)
}

func (o *OrderResource) DeleteOrder(c echo.Context) error {

	id := c.Param("id")

	err := o.cache.delete(id)
	if err != nil {
		return c.String(500, err.Error())
	}

	return c.String(200, "delete success")
}
